﻿using System;
using System.Text;

namespace MySqlManager
{
    /// <summary>
    /// Layer class over a MySqlCommand used to delete data from a MySQL database table
    /// </summary>
    public class MySqlDelete : SqlQuery, IDisposable
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="MySqlDelete"/> class
        /// </summary>
        public MySqlDelete()
            : base()
        {
            this.executeDelegate = this.ExecuteNonQuery;
        }

        /// <summary>
        /// Release all resources used by the MySqlDelete
        /// </summary>
        public void Dispose()
        {
            this.Dispose(true);
        }

        /// <summary>
        /// Release the resources used by the MySqlDelete
        /// </summary>
        /// <param name="disposeNative">true if native objects should be disposed</param>
        private void Dispose(bool disposeNative)
        {
            if (disposeNative)
            {
                //dispose any native objects here
            }
            this.mySqlCommand.Dispose();
            GC.SuppressFinalize(this);
        }

        /// <summary>
        /// Method called by the SQLQuery base methods Execute and BeginExecute
        /// </summary>
        private void ExecuteNonQuery()
        {
            StringBuilder cmd = new StringBuilder("Delete From ");
            cmd.Append(this.tableName);
            this.AppendWhereStatments(cmd);
            this.mySqlCommand.CommandText = cmd.ToString();
            using (this.mySqlCommand.Connection = MySqlCore.GetConnection())
            {
                this.mySqlCommand.ExecuteNonQuery();
            }
        }
    }
}
